using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuffController : MonoBehaviour
{
    // 二进制buff状态
    private int gameBuffState;
    private Dictionary<PropType, float> buffTimeStampDic = new Dictionary<PropType, float>();

    private void Start() {
        gameBuffState = 0;
        buffTimeStampDic.Clear();
    }

    public void UpdateBuffState(PropType type, float duration)
    {
        int state = (1<<((int)type));
        gameBuffState |= state;
        
        if(buffTimeStampDic.ContainsKey(type))
        {
            buffTimeStampDic[type] = Mathf.Max(buffTimeStampDic[type], Time.time) + duration;
        }
        else
        {
            buffTimeStampDic.Add(type, Time.time + duration);
        }
    }

    public int GetGameBuffState()
    {
        return gameBuffState;
    }

    public float GetBuffTimeStampByType(PropType type)
    {
        if(buffTimeStampDic.ContainsKey(type))
        {
            return buffTimeStampDic[type];
        }

        return 0f;
    }

    public void SubBuffByType(PropType type)
    {
        gameBuffState &= ~(1<<((int)type));
    }

    public bool isHasBuff(PropType type)
    {
        int buff = (1<<((int)type));
        bool hasBuff = ((buff & gameBuffState) == 1);

        return hasBuff;
    }

    public bool isInBuffDuring(PropType type)
    {
        if(buffTimeStampDic.ContainsKey(type))
        {
            float timeStamp = buffTimeStampDic[type];
            return Time.time < timeStamp - 0.5;
        }

        return false;
    }
}
